import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:oster_cigarette/common/const/const.dart';
import 'package:oster_cigarette/generated/l10n.dart';
import 'package:oster_cigarette/pages/home/home_page.dart';
import 'package:oster_cigarette/pages/login/login_page.dart';
import 'package:oster_cigarette/router/generate_route.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
  runApp(const ProviderScope(child: MyApp()));
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        highlightColor: Colors.transparent,
        scaffoldBackgroundColor: Colours.white,
        splashColor: Colors.transparent,
        appBarTheme: const AppBarTheme(
          elevation: 0,
          backgroundColor: Colours.fff1f3f9,
          surfaceTintColor: Colors.transparent,
          titleTextStyle: TextStyle(
            fontSize: 18,
            height: 20 / 18,
            fontWeight: FontWeight.bold,
            color: Colours.mainTextColor,
          ),
        ),
      ),
      onGenerateRoute: GRouter.getRoute,
      localizationsDelegates: const [
        S.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
      supportedLocales: S.delegate.supportedLocales,
      home: const KeyboardDismissOnTap(
        dismissOnCapturedTaps: true,
        child: MyHomePage(),
      ),
      builder: (BuildContext context, Widget? child) {
        /// make sure that loading can be displayed in front of all other widgets
        return MediaQuery(
          data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
          child: FlutterEasyLoading(child: child),
        );
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Future<Widget> _init() async {
    final instance = await SharedPreferences.getInstance();
    final isFirst = instance.getBool(AppConst.is_first) ?? true;
    if (isFirst) {
      instance.setBool(AppConst.is_first, false);
      return Container(
        child: Text("SpalshPage"),
      );
    }
    if (instance.getBool(AppConst.is_login) ?? false) {
      return const HomePage();
    } else {
      return const LoginPage();
    }
  }

  @override
  void initState() {
    super.initState();
    _im();
  }

  void _im() async {
    // var options = EMOptions(appKey: "easemob-demo#easeim");
    // await EMClient.getInstance.init(options);
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTextStyle(
      style: TextStyle(
        color: Colours.mainTextColor,
      ),
      child: FutureBuilder<Widget>(
        future: _init(),
        initialData: Container(),
        builder: (context, snapshot) => snapshot.data ?? const SizedBox(),
      ),
    );
  }
}
